<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language
//Người tạo	: Hồ Nhật Tân - Team 1
//Chỉnh sửa : Bùi Thành Nguyên - Team 4
//Module 	: Contact
//Chức năng	: Tạo trang gửi mail liên hệ

class ContactForm_IndexController {

	public function init(){
		parent::init();
	}

	public function indexAction()
	{
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/contactform');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
		
		//Start------Khai báo các parameter
		$path = APPLICATION_PATH."/widgets/contactform/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Khai báo các parameter
		
		/************ Start check user login *********/
		$session1 = new Zend_Session_Namespace('login.auth');
		if(isset($session1->id_user))
		{
			$this->view->login = "1";
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_content($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		
		//Start------Khởi tạo mã captcha
		$captcha = new Zend_Captcha_Image();
		$captcha	->setTimeout('300')
	                ->setWordLen('6')
	                ->setHeight('50')
	                ->setWidth('160')
					->setImgDir(APPLICATION_PATH . '/../public/captcha/images/')
	                ->setImgUrl('/captcha/images/')
	                ->setSuffix('.jpg')
	                ->setFont(APPLICATION_PATH . '/../public/captcha/font/jkasperville.ttf')
	                ->setFontSize('30')
	                ->setLineNoiseLevel('5')
	            	->setDotNoiseLevel('50')
					->setExpiration('1')
	           	 	->setGcFreq('1');
		$captcha->generate();
		$this->view->captcha = $captcha->render(); 
		$this->view->captchaID = $captcha->getId();
		//End--------Khởi tạo mã captcha
				
		//Start------Đưa chuỗi captcha vào session
		$captchaSession = new Zend_Session_Namespace('Zend_Captcha_' . $captcha->getId());
		$captchaSession->word = $captcha->getWord();
		//End--------Đưa chuỗi captcha vào session	
		
		
		
		//Start------Xử lý gửi mail
		if (isset($_POST["fullname"])&&isset($_POST["email"])&&isset($_POST["subject"])&&isset($_POST["message"])&&isset($_POST["captcha"])&&isset($_POST["hdnCaptcha"]))
		{
			//Start------Lấy dữ liệu
	        $flag = true;
	        $fullname 		= $_POST["fullname"];
	        $email 			= $_POST["email"];
	        $subject 		= $_POST["subject"];
	        $message 		= $_POST["message"];
	        $captcha_input 	= $_POST["captcha"];
	        $captcha_hd		= $_POST["hdnCaptcha"];
	        
		    $html = "<b>You have received a new contact mail. "."Here are the details:</b><br/> <b>Name:</b> $fullname <br/> <b>Email:</b> $email <br/> <b>Message:</b> <br/>".nl2br($message);
		    $email_to		= "aibietdau111@gmail.com";
		    $password		= "naythipass";
	        //End--------Lấy dữ liệu

			//Start------Lấy thông tin captcha từ session
         	$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captcha_hd);
         	$captchaIterator = $captchaSession->getIterator();
         	$captchaWord = $captchaIterator['word'];
         	//End--------Lấy thông tin captcha từ session
         	
         	//Start------Kiểm tra captcha nhập từ form
			if(strtolower($captcha_input) != strtolower($captchaWord)){
				$flag = false;
				//Start------Trả dữ liệu về form qua view
				$this->view->captchaError	= false;
			    $this->view->fullname 		= $fullname;
			    $this->view->email 			= $email;
			    $this->view->subject 		= $subject;
			    $this->view->message 		= $message;
			    //End--------Trả dữ liệu về form qua view
			}
			//End--------Kiểm tra captcha nhập từ form
			
			//Start------Gửi mail
			if($flag==true)
			{
				$config = array('auth' => 'login', 
								'username' => $email_to, 
								'password' => $password, 
								'ssl' => 'ssl', 
								'port' => 465 );
				$tr = new Zend_Mail_Transport_Smtp ('smtp.gmail.com', $config);
	         	Zend_Mail::setDefaultTransport ($tr);
				$mail 	= new Zend_Mail('UTF-8');
			    $mail 	->setBodyHtml($html);
			    $mail	->setFrom($email_to);
			    $mail	->addTo($email_to);
			    $mail	->setSubject($subject);
			    $mail	->setReplyTo($email, $fullname);
			    
				try
				{
					$mail->send();
					$this->view->flag = "success";
				}
				catch(Zend_Mail_Exception $e)
				{
					$this->view->flag = "fail";
				}
			}
			//End--------Gửi mail
			
			
		}	
		
		//Start------Truyền parameter qua View
		$titleXML = 'title'.$name['name'];
        if(isset($xmlconfig->$titleXML))
        	$this->view->title = $xmlconfig->$titleXML;
        else
        	$this->view->title = $xmlconfig->titleDefault;

        /*========== Start Get Text ==============*/
        $this->view->text_name 			= $language->get('text_name');
		$this->view->text_require 		= $language->get('text_require');
		$this->view->text_require1 		= $language->get('text_require1');
		$this->view->text_email			= $language->get('text_email');
		$this->view->text_content 		= $language->get('text_content');
		$this->view->text_captcha 		= $language->get('text_captcha');
		$this->view->text_enter 		= $language->get('text_enter');
		$this->view->text_btn_send 		= $language->get('text_btn_send');
		$this->view->text_title 		= $language->get('text_title');
		$this->view->text_lang 			= $language->get('text_lang');
		$this->view->text_captcha_test	= $language->get('text_captcha_test');
		/*========== End Text ==============*/
		//End--------Truyền parameter qua View
	}
		
}